<template>
<form @submit="selectLoginFun">
	<image mode='widthFix' class='app_login_bg' src='https://image1.sansancloud.com/xianhua/2019_11/26/9/32/2_137.jpg?x-oss-process=style/preview_120'></image>
	<view class='app_login_container'>
	  <view class='app_logo_block'>
	    <image mode='aspectFit' class='app_logo_img' :src='setting.platformSetting.logo&&setting.platformSetting.logo!="undefined"?setting.platformSetting.logo:"https://image1.sansancloud.com/sansancloud/2018_7/14/11/21/37_175.jpg?x-oss-process=style/preview_120"'></image>
	  </view>
	  <view class='app_input_block'>
	    <view class='app_input_item'>
	      <image class='app_input_item_icon' src='https://image1.sansancloud.com/xianhua/2019_11/26/10/22/26_966.jpg?x-oss-process=style/preview_120'></image>
	      <input placeholder='请输入您的手机号' name="username" style='width:80%;' v-model="telno"></input>
	    </view>
	    <view class='app_input_item' v-if="loginType=='normal'">
	      <image class='app_input_item_icon' src='https://image1.sansancloud.com/xianhua/2019_11/26/10/22/26_968.jpg?x-oss-process=style/preview_120'></image>
	      <input placeholder='请输入您的密码' name="password" password style='width:80%;'></input>
	    </view>
        <view class='app_input_item verify_code' v-else>
          <view class="app_input_item_left">
              <image class='app_input_item_icon' src='https://image1.sansancloud.com/xianhua/2019_11/26/10/22/26_968.jpg?x-oss-process=style/preview_120'></image>
              <input placeholder='请输入验证码' name="code" style='width:50%;' v-model="verifyCode"></input>
          </view>
          <button class="gain_verification_code" :loading="verifyLoading" @tap="getVerifyCodeFun">
              <text v-if="!verifyStatus&&waitingTime==0">获取验证码</text>
              <text v-else style="color: #999;">重新发送({{waitingTime}})</text>
          </button>
        </view>
	  </view>
	  <view class='app_botton_block'>
	     <button class='app_botton_item login' :style="{background:setting.platformSetting?setting.platformSetting.defaultColor:'#2269d5',border:'1rpx solid'+(setting.platformSetting?setting.platformSetting.defaultColor:'#2269d5')}" form-type="submit" v-if="!loginState">登录</button>
	     <button class='app_botton_item login' loading="true" :style="{background:setting.platformSetting?setting.platformSetting.defaultColor:'#2269d5',border:'1rpx solid'+(setting.platformSetting?setting.platformSetting.defaultColor:'#2269d5')}" v-else>登录中...</button>
	     <button class='app_botton_item register' @tap.stop="toRegistFun" :style="{color:setting.platformSetting?setting.platformSetting.defaultColor:'#2269d5',border:'1rpx solid'+(setting.platformSetting?setting.platformSetting.defaultColor:'#2269d5')}">注册</button>
	     <view class="bottom_fun">
             <view class='forget_password' @tap="changeQuickFun">
               <text v-if="loginType=='normal'">手机快捷登录</text>
               <text v-else>账号密码登录</text>
             </view>
             <view class='forget_password' @tap="forgetPasswordFun">
               <text>忘记密码</text>
               <image class='forget_password_icon' src='https://image1.sansancloud.com/xianhua/2019_11/26/10/22/27_268.jpg?x-oss-process=style/preview_120'></image>
             </view>
         </view>
	  </view>
	</view>
</form>
</template>

<script>
import { socketFun } from "../../wxcomponents/public/json2Form";
export default {
  data() {
    return {
      loginData:{},
	  loginState:false,
      verifyStatus:false,
      verifyLoading:false,
	  setting:{},
      loginType:'quick',
      waitingTime:0,
      telno:'',
      verifyAnimation:null,
      backUrl:'',
    };
  },
  props: {},

  /*onload*/
  onLoad: function (options) {
    // wx.hideTabBar({})
    let that=this;
    console.warn("======onLoad:options======", options, getApp().globalData.setting);
    if(options.url){
        that.setData({backUrl:options.url})
    }
	that.setData({setting:getApp().globalData.setting})
  },
  onReady: function () {
    
  },
  onShow: function () {
    let that=this;
	that.loginState=false;
  },
  /* 分享 app.js862行*/
  onShareAppMessage: function () {
    console.log(getApp().globalData.miniIndexPage);
    return getApp().globalData.shareForFx2(getApp().globalData.miniIndexPage);
  },
  onPullDownRefresh: function () {
    let that = this; // 下拉刷新的时候首先判断存不存在tab
    console.log("下拉");
    that.refreshPage();
    wx.stopPullDownRefresh();
  },
  methods: {
    changeQuickFun:function(){
        let that=this;
        console.log("====changeQuickFun====")
        if(that.loginType=='quick'){
            that.setData({loginType:'normal'})
        }else{
            that.setData({loginType:'quick'})
        }
    },
    forgetPasswordFun:function(){
        let that=this;
        console.log("====forgetPasswordFun====");
		let linkUrl="app_repassword_page.html"
		getApp().globalData.linkEvent(linkUrl);
    },
	toRegistFun:function(){
		let that=this;
		let linkUrl="app_regist_page.html"
		getApp().globalData.linkEvent(linkUrl);
	},
	selectLoginFun:function(e){
	  let that=this;
	  console.log("====loginFun====",e)
	  let params=e.detail.value||{};
	  console.log("====params====",params)
	  if(that.loginType=='normal'){
          that.loginFun(params)
      }else{
          that.quickLoginFun(params)
      }
	},
    loginFun:function(params){
        let that=this;
        that.loginState=true;
        params=Object.assign({},params,{scene:getApp().globalData.more_scene||0})
        getApp().globalData.toLogin('/login.html?jsonOnly=1',params,function(res){
            console.log("====== res.data=========", res.data);
            wx.hideLoading();
            let resData=res.data
            if (!resData.errcode || resData.errcode == '0') {
              that.loginSuccessFun(resData,params)
            } else {
              that.loginState=false;
              that.loginFailFun(resData)
            }
        },function(res){
            console.log(res);
            wx.hideLoading();
            that.loginState=false;
            that.loginFailFun()
        })
    },
    quickLoginFun:function(params){
        let that=this;
        params=Object.assign({},params,{telno:params.username,scene:getApp().globalData.more_scene||0})
        console.log("===quickLoginFun====",params)
        that.loginState=true;
        getApp().globalData.toLogin('/login_with_telno_code.html?jsonOnly=1',params,function(res){
            console.log("====== res.data=========", res.data);
            wx.hideLoading();
            let resData=res.data
            if (!resData.errcode || resData.errcode == '0') {
              that.loginSuccessFun(resData,params)
            } else {
              that.loginState=false;
              that.loginFailFun(resData)
            }
        },function(res){
            console.log(res);
            wx.hideLoading();
            that.loginState=false;
            that.loginFailFun()
        })
    },
    loginSuccessFun:function(resData,params){
        console.log("====loginSuccessFun===",resData);
        let that=this;
        let loginToken=resData.relateObj.platformUser.loginToken;
        that.$set(params,'loginToken',loginToken)
        getApp().globalData.setStorageFun("loginToken",loginToken)
        getApp().globalData.setStorageFun("loginParams",params)
        that.setData({
            loginData:resData.relateObj
        });
        if(that.backUrl){
            getApp().globalData.linkEvent(that.backUrl,'reLaunch');
        }else{
            getApp().globalData.toIndex('reLaunch');
        }
    },
    loginFailFun:function(resData){
        console.log("====loginFailFun===",resData);
        let text=resData&&resData.errMsg?resData.errMsg:'登录失败，点击【确定】重新登录';
        wx.showModal({
            title: '提示',
            content: text,
            success: function (res) {
              if (res.confirm) {
              } else if (res.cancel) {
                // getApp().globalData.toIndex();
              }
            }
        });
    },
	getVerifyCodeFun:function(){
	  let that=this;
      console.log("===getVerifyCodeFun====",that.telno);
      let params={
          telno:that.telno,
          type:3
      }
      if(!that.telno){
        wx.showModal({
            title: '提示',
            content: '请您填写手机号！',
            success: function (res) {
              if (res.confirm) {
                  that.setData({verifyLoading:false})
              } else if (res.cancel) {
                  that.setData({verifyLoading:false})
              }
            }
        });
        return
      }
      that.waitingTime=60
      let clock =setInterval(function () {
        that.waitingTime--;
        if(that.waitingTime<=0){
            that.waitingTime=0;
            clearInterval(clock);
        }
      },1000)
	  let customIndex = getApp().globalData.AddClientUrl("/get_verify_code.html", params, 'post', '1'); //拿custom_page
	  console.log("customIndex",customIndex)
	  uni.request({
		url: customIndex.url,
		data: customIndex.params,
		method: 'POST',
		header: getApp().globalData.headerPost,
		success: function (res) {
		  console.log("====== res.data=========", res.data);
		  wx.hideLoading();
          that.setData({verifyLoading:false})
		  if (!res.data.errcode || res.data.errcode == '0') {
            getApp().globalData.showToastFun("手机验证码已发送，请查看手机！",2000,"none")
		  } else {
            let text=res.data.errMsg||'注册失败，点击【确定】重新注册';
			wx.showModal({
				title: '提示',
				content: text,
				success: function (res) {
				  if (res.confirm) {
				  } else if (res.cancel) {
					// getApp().globalData.toIndex();
				  }
				}
			});
		  }
		},
		fail: function (res) {
		  console.log(res);
		  wx.hideLoading();
		  wx.showModal({
			title: '提示',
			content: '获取失败，点击【确定】重新获取',
			success: function (res) {
			  if (res.confirm) {
				// that.getParac();
			  } else if (res.cancel) {
				// getApp().globalData.toIndex();
			  }
			}
		  });
		}
	  });
	},
    setNavBar: function () {
      console.warn("1111111111111",getApp().globalData.setting);

      if (getApp().globalData.setting.platformSetting&&getApp().globalData.setting.platformSetting.siteTitle == '') {
        wx.setNavigationBarTitle({
          title: '首页'
        });
      } else {
        wx.setNavigationBarTitle({
          title: getApp().globalData.setting.platformSetting?getApp().globalData.setting.platformSetting.siteTitle:'默认首页'
        });
      }

      wx.setNavigationBarColor({
        frontColor: getApp().globalData.setting.platformSetting?getApp().globalData.setting.platformSetting.topColor.toLowerCase():'',
        backgroundColor: getApp().globalData.setting.platformSetting?getApp().globalData.setting.platformSetting.topBgColor:''
      });
    },
    getStateData: function (state) {
      let that = this;
      console.log("===getStateData===", state, that.showAuthorizationPopup);
      let partialsName = JSON.parse(that.partialsName);
      partialsName.state = state;
      that.setData({
        partialsName: null
      });
      setTimeout(function () {
        that.setData({
          partialsName: JSON.stringify(partialsName)
        });
      }, 300);
    },
    jumpToPage: function (shareParam) {
      if (shareParam.pageName == 'custom_page_index') {
        return;
      } else if (shareParam.pageName == 'shopping_car_list') {
        return;
      } else if (shareParam.pageName == 'custom_page_userinfo') {
        return;
      } else {
        let paramStr = getApp().globalData.jsonToStr(shareParam);
        wx.navigateTo({
          url: '/pages/' + shareParam.pageName + '/index' + paramStr
        });
      }
    },
    onPageScroll: function (object) {
		console.log('=====positionNotifyer==')
    },

    /* 组件事件集合 */
    tolinkUrl: function (e) {
      console.warn("=======e=======", e);
      let linkUrl = e.currentTarget.dataset.link;
      getApp().globalData.linkEvent(linkUrl);
    },
    setPageName: function (urlStr) {
      let url = urlStr || "";
      let partialsName = "";

      if (url) {
        partialsName = JSON.stringify({
          url: url,
          state: this.showAuthorizationPopup
        });
      }

      this.setData({
        partialsName: partialsName
      });
      console.log("partialsName", this.partialsName);
    },
    refreshPage: function () {
      let that = this;
      console.log("首页刷新");
      that.setPageName();
      wx.showToast({
        title: '加载中...',
        icon: 'none',
        duration: 500
      });
      setTimeout(function () {
        that.setPageName('index');
        getApp().globalData.footerCount = 0;
      }, 500);
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>